const http=require('http')
const path=require('path')
const fs=require('fs')
const url=require('url')

var reqUrl='http://nodejs.cn/'
var filePath=path.join(__dirname,'/http-request.html')
var writable=fs.createWriteStream(filePath);

//url.parse(url) 可以将url的字符串转换为对象结构
var obj=url.parse(reqUrl)

var options={
    hostname:'nodejs.cn',
    port:80,
    path:'/',
    method:'GET'
}

var request=http.request(options,(res)=>{
    var str=''
    res.on('data',(chunk)=>{
        str+=chunk 
    })
    res.on('end',()=>{
        console.log(str)
        writable.write(str)
    })
})

//http.request方法一定要调用end方法
request.end();